<?php

class Euso_ObjectClass
{
	protected $_idname = NULL;
	protected $_className = NULL;
	protected $_registry = NULL;
	protected $_moduleName = NULL;


    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }


	public function setIdName($idname = NULL)
	{
		$this->_idname = $idname;
		return $this;
	}
	public function setClassName($className = NULL)
	{
		$this->_className = $className;
		return $this;
	}
	public function setRegistry($objectRegistry = NULL)
	{
		$this->_registry = $objectRegistry;
		return $this;
	}
	public function setModuleName($moduleName=NULL)
	{
		$this->_moduleName = $moduleName;
		return $this;
	}

	public function getIdName()
	{
		return $this->_idname;
	}
	public function getClassName()
	{
		return $this->_className;
	}
	public function getRegistry()
	{
		return $this->_registry;
	}
	public function getModuleName()
	{
		return $this->_moduleName;
	}


    public function __set($name, $value)
    {
        $method = 'set' . ucfirst($name);
        $this->$method($value);
    }
    public function __get($name)
    {
        $method = 'get' . ucfirst($name);
        return $this->$method();
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }



	protected static $_instanceList = array();


	/**
	 * Queries for an instance. If the instance is not found, it will be loaded from the database.
	 */
	public static function getInstanceByIdName($classIdName = NULL)
	{
		if (empty($classIdName) === FALSE && isset(self::$_instanceList[$classIdName])) {
			return self::$_instanceList[$classIdName];
		}
		return NULL;
	}
	public static function getInstanceByClassName($className = NULL)
	{
		foreach (self::$_instanceList as $inst) {
			if ($inst->getClassName() == $className)
				return $inst;
		}
		return NULL;
	}

	public static function getInstanceClassName($idName) {
		$inst = self::getInstanceByIdName($idName);
		return $inst === NULL ? NULL : $inst->getClassName();
	}
	public static function getInstanceIdName($className) {
		$inst = self::getInstanceByClassName($className);
		return $inst === NULL ? NULL : $inst->getIdName();
	}

	public static function register($classIdName = NULL, $className = NULL, $factoryClassName = NULL, $moduleName = NULL, $replace = FALSE)
	{
		if (empty($classIdName)) {
			throw new Exception('Empty class id name');
		}
		if (empty($className)) {
			throw new Exception('Empty class name');
		}
		if (isset(self::$_instanceList[$classIdName]) === FALSE || $replace === TRUE) {
			$instance = new Euso_ObjectClass();
			$instance
				->setIdName($classIdName)
				->setClassName($className)
				->setModuleName($moduleName)
			;
			self::$_instanceList[$classIdName] = $instance;
		}
		return $this;
	}
}